/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.InventoryReportEntity;
import entity.LocationQuantityEntity;
import entity.ReservationEntity;
import entity.SalesOrderEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Andy
 */
@Stateless
public class InventoryReportEntityFacade extends AbstractFacade<InventoryReportEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;
    private int totalClassicNutriBarNuts;
    private int totalClassicNutriBarFruits;
    private int totalPremiumPowerBarNuts;
    private int totalPremiumPowerBarFruits;
    @EJB
    CompletedGoodsEntityFacade completedGoodsEntityFacade;
    @EJB
    SalesOrderEntityFacade salesOrderEntityFacade;
    List<LocationQuantityEntity> locationQuantities = new ArrayList<LocationQuantityEntity>();

    protected EntityManager getEntityManager() {
        return em;
    }

    public InventoryReportEntityFacade() {
        super(InventoryReportEntity.class);
    }

    @Schedule(minute = "*/1", hour = "*")
    public void createSchedule() {
        System.out.println("schedule working");

        InventoryReportEntity inventoryReportEntity = new InventoryReportEntity();
        inventoryReportEntity.setTotalClassicFruits(this.getTotalClassicNutriBarFruits());
        inventoryReportEntity.setTotalClassicNuts(this.getTotalClassicNutriBarNuts());
        inventoryReportEntity.setTotalPremiumNuts(this.getTotalPremiumPowerBarNuts());
        inventoryReportEntity.setTotalPremiumFruits(this.getTotalPremiumPowerBarFruits());
        inventoryReportEntity.setUtilizationReport((this.getTotalClassicNutriBarFruits()+this.getTotalClassicNutriBarNuts()+this.getTotalPremiumPowerBarNuts()+this.getTotalPremiumPowerBarFruits())/150);
        em.persist(inventoryReportEntity);

    }

    public int getTotalClassicNutriBarNuts() {
        totalClassicNutriBarNuts = 0;

        locationQuantities = completedGoodsEntityFacade.findAllCG();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
            if (productName.equals("Classic Nutri Bar (Nuts)")) {
                if (locationQuantity.isArchived() == false) {
                    totalClassicNutriBarNuts = totalClassicNutriBarNuts + locationQuantity.getQuantity();
                }
            }
        }
        return totalClassicNutriBarNuts;
    }

    public int getTotalClassicNutriBarFruits() {
        totalClassicNutriBarFruits = 0;

        locationQuantities = completedGoodsEntityFacade.findAllCG();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
            if (productName.equals("Classic Nutri Bar (Fruits)")) {
                if (locationQuantity.isArchived() == false) {
                    totalClassicNutriBarFruits = totalClassicNutriBarFruits + locationQuantity.getQuantity();
                }
            }
        }
        return totalClassicNutriBarFruits;
    }
    
       public int getTotalPremiumPowerBarFruits() {
        totalPremiumPowerBarFruits = 0;

        locationQuantities = completedGoodsEntityFacade.findAllCG();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
            if (productName.equals("Premium Power Bar (Redcurrant Fruits)")) {
                if (locationQuantity.isArchived() == false) {
                    totalPremiumPowerBarFruits = totalPremiumPowerBarFruits + locationQuantity.getQuantity();
                }
            }
        }
        return totalPremiumPowerBarFruits;
    }
       
          public int getTotalPremiumPowerBarNuts() {
        totalPremiumPowerBarNuts = 0;

        locationQuantities = completedGoodsEntityFacade.findAllCG();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
            if (productName.equals("Premium Power Bar (Chocolate Nuts)")) {
                if (locationQuantity.isArchived() == false) {
                    totalPremiumPowerBarNuts = totalPremiumPowerBarNuts + locationQuantity.getQuantity();
                }
            }
        }
        return totalPremiumPowerBarNuts;
    }
}
